<template>
	<view>
		<cu-custom bgColor="bg-color" :isBack="true">
			<view slot="backText">返回</view>
			<block slot="content">登录</block>
		</cu-custom>
		<view>
			<view class='header'>
				<image :src='ossUrl+"logo.png"'></image>
			</view>
			<view class='content'>
				<view>申请获取以下权限</view>
				<text>获得你的公开信息(昵称，头像等)</text>
			</view>
			<view>
				<button class='bottom' v-show="!userInfo.openid" type='primary' open-type="getUserInfo" lang="zh_CN" @getuserinfo="getUserInfo">
					授权登录
				</button>
				<button class='bottom' @getphonenumber="getphonenumber" v-show="!userInfo.mobile&&userInfo.openid" type='primary'
				 open-type="getPhoneNumber">
					获取手机号
				</button>
			</view>

		</view>
	</view>
</template>

<script>
	import {
		request,
		ossUrl
	} from '../util/ajax.js'
	import {
		redirectLastPath
	} from '../util/utils.js'
	import {
		mapState,
		mapMutations,
		mapGetters
	} from 'vuex';
	export default {
		data() {
			return {
				ossUrl: ossUrl
			}
		},
		onLoad() {},
		computed: {
			...mapState(['userInfo'])
			//...mapGetters(['userInfo'])
		},
		methods: {
			...mapMutations(['login']),
			getphonenumber(e) {
				let that = this;
				let data = {
					encryptedData: e.detail.encryptedData,
					iv: e.detail.iv,
					sessionKey: that.userInfo.sessionKey
				}
				request({
					methods: "POST",
					url: "auth/addMobile",
					data: data
				}, 'query').then(res => {
					console.log(res)
					if (res.data.code == 0) {
						console.log(res)
						that.userInfo.mobile = res.data.purePhoneNumber;
						that.login({
							userInfo: that.userInfo,
							sysUser: res.data.sysUser
						})
						uni.showToast({
							title: '获取成功',
							complete() {
								redirectLastPath();
							}
						});

					}
				})

			},
			getUserInfo(e) {
				let _this = this;
				let ret = e.detail;
				if (ret.errMsg == "getUserInfo:fail auth deny") {
					uni.showModal({
						title: '提示',
						content: '为了更好的服务，请同意授权',
						showCancel: false,
						success: function(res) {
							if (res.confirm) {
								console.log('用户点击确定');
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				} else if (ret.errMsg == "getUserInfo:ok") {
					_this.userLogin(e.detail);
				} else {
					uni.showToast({
						title: '授权失败',
						duration: 2000
					});
				}
			},
			userLogin(e) {
				let _this = this;
				uni.login({
					provider: 'weixin',
					success: function(res) {
						let code = res.code;
						// 获取用户信息
						uni.getUserInfo({
							provider: 'weixin',
							success: function(infoRes) {
								//	console.log(infoRes);
								let data = {
									code: code,
									signature: infoRes.signature,
									iv: infoRes.iv,
									encryptedData: infoRes.encryptedData,
									rawData: infoRes.rawData,
								}
								//调登录接口
								request({
									methods: 'POST',
									url: 'auth/login_by_weixin',
									data
								}, 'query').then(res => {
									console.log("登录成功回调", res);
									if (res.data.code == 0) {
										_this.login({
											userInfo: res.data.userInfo,
											token: res.data.token,
											sysUser: res.data.sysUser,
											sysMenus:res.data.sysMenus,
										})
										uni.showToast({
											title: '登录成功',
											complete() {
												if (res.data.userInfo.mobile) {
													redirectLastPath();
												}
											}
										});

									}
								})
							}
						});
					}
				});

			},
		}
	}
</script>

<style>
	.bg-color {
		background-color: white !important;
	}

	.header {
		margin: 90rpx 0 90rpx 50rpx;
		border-bottom: 1px solid #ccc;
		text-align: center;
		width: 650rpx;
		height: 300rpx;
		line-height: 450rpx;
	}

	.header image {
		width: 200rpx;
		height: 200rpx;
	}

	.content {
		margin-left: 50rpx;
		margin-bottom: 90rpx;
	}

	.content text {
		display: block;
		color: #9d9d9d;
		margin-top: 40rpx;
	}

	.bottom {
		border-radius: 80rpx;
		margin: 70rpx 50rpx;
		font-size: 35rpx;
	}
</style>
